import math
a = 0
def IsPrime (a):
    if a == 1 :
        return 1
    else:
        q = 0
        s = math.sqrt(a)
        for i in range (2,(round(s))+2):
            if a % i == 0 :
                q += 1
        if q == 0 :
            return 1
        else:
            return 0
d = 10
C = 0
b=int(input())
if b < 1000 or b > 9999:
    print(0)
else:
    a = b
    if IsPrime(a) == 1:
        print(b)
    else:
        for i in range (3):
            a = b % d
            if IsPrime(a) == 1 and a > C:
                C = a
            a = b // d
            if IsPrime(a) == 1 and a > C:
                C = a
            d = d*10
        a = (b // 10) % 100
        if IsPrime(a) == 1 and a > C:
            C = a
        print(C)